package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.shwread.android.qysw10000243.R;

import java.util.List;

public class ContactCallSelectDialog
        extends
        Dialog
{
	public interface onSelectSexListener
	{
		void onSelect(int location);
	}
	
	private Context      context;
	//	private List<ZoneInfo>		listZones;
	private List<String> phoneList;
	private ListView     lv;
	private Button       btnCancel;
	private MyAdapter    adapter = new MyAdapter();
	
	public ContactCallSelectDialog(Context context, List<String> phoneList) {
		super(context, R.style.common_dialog);
		setCanceledOnTouchOutside(true);
		this.context = context;
		this.phoneList = phoneList;
		findViews();
		setListeners();
		
		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.order_seat_location_dialog, null);
		lv = (ListView) view.findViewById(R.id.lv_order_seat_location);
		if (phoneList.size() <= 2)
		{
			lv.setVerticalScrollBarEnabled(false);
		}
		else
		{
			lv.setVerticalScrollBarEnabled(true);
		}
		btnCancel = (Button) view.findViewById(R.id.btn_order_seat_location_cancel);
		setContentView(view);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener()
		{
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				dialPhoneNumber(phoneList.get(arg2));
				dismiss();
			}
		});
	}
	
	private class MyAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return phoneList.size();
		}
		
		@Override
		public Object getItem(int arg0) {
			return null;
		}
		
		@Override
		public long getItemId(int arg0) {
			return 0;
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View view;
			if (arg1 == null)
			{
				view = View.inflate(context, R.layout.contact_call_dialog_list_item, null);
			}
			else
			{
				view = arg1;
			}
			TextView tvPhoneNum = (TextView) view.findViewById(R.id.tv_contact_call);
			tvPhoneNum.setText(phoneList.get(arg0));
			return view;
		}
		
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		btnCancel.setOnClickListener(l);
	}
	
	/**
	 * 调起系统拨号盘
	 * 
	 * @param phoneNumber
	 */
	public void dialPhoneNumber(String phoneNumber) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:" + phoneNumber));
		context.startActivity(intent);
	}
	
	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
				case R.id.btn_order_seat_location_cancel:
					dismiss();
					break;
			}
		}
	}
	
}
